package com.schedualnew.mq.yihuiesb.model;

import java.util.Iterator;
import java.util.Map;

import com.schedualnew.mq.util.MarshallerUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


public class EsbMsgParser {
    public static EsbReceiveMsg parseCommonEsbMsg(String inputString, StringBuffer result) {
        if (StringUtils.isEmpty(inputString)) {
            result.append("返回消息为空");
            return null;
        }

        inputString = inputString.replaceAll("<DETAIL>", "<DETAIL><![CDATA[<msg><body><row action=\"select\">");
        inputString = inputString.replaceAll("</DETAIL>", "</row></body></msg>]]></DETAIL>");

        System.out.println("inputString = " + inputString);
        MarshallerUtils marshallerUtils = new MarshallerUtils();
        EsbReceiveMsg esbMsg = (EsbReceiveMsg) marshallerUtils.parseXmlDataObject(EsbReceiveMsg.class, inputString);
        if (esbMsg == null) {
            result.append("解析集成平台返回消息失败");
            return null;
        }
        MsgContent a = esbMsg.getMsgInfo().getMsg();
        DetailsContent s = esbMsg.getMsgInfo().getMsg().getDetails();
        if (esbMsg.getMsgInfo() == null || esbMsg.getMsgInfo().getMsg().getDetails().getContentList() == null || esbMsg
                .getMsgInfo().getMsg().getDetails().getContentList().isEmpty()) {
            result.append("集成平台返回消息未找到有效内容");
            return null;
        }

        return esbMsg;
    }

    public static EsbApplyReceiveMsg parseApplyEsbMsg(String inputString, StringBuffer result) {
        if (StringUtils.isEmpty(inputString)) {
            result.append("返回消息为空");
            return null;
        }

        MarshallerUtils marshallerUtils = new MarshallerUtils();
        EsbApplyReceiveMsg esbMsg = (EsbApplyReceiveMsg) marshallerUtils.parseXmlDataObject(EsbApplyReceiveMsg.class, inputString);
        if (esbMsg == null) {
            result.append("解析集成平台返回消息失败");
            return null;
        }

        if (esbMsg.getMsgInfo() == null || esbMsg.getMsgInfo().getMsg() == null || esbMsg.getMsgInfo().getMsg().getDetails().contentList.isEmpty()) {
            result.append("集成平台返回消息未找到有效内容");
            return null;
        }

        return esbMsg;
    }


    public static boolean parsePushResult(String inputString, String resultCode, StringBuffer resultInfo) throws DocumentException {
        Document document = DocumentHelper.parseText(inputString);
        if (document == null) {
            return false;
        }

        Element root = document.getRootElement();
        if (root == null) {
            return false;
        }
        Element msgElement = root.element("MsgInfo").element("Msg");
        if (msgElement == null) {
            return false;
        }
        resultCode = msgElement.elementText("RESULT_CODE");
        resultInfo.append(msgElement.elementText("NOTE"));
        return true;
    }


    public static boolean parseDetailMsg(String inputString, Map<String, String> msgInfoMap) throws DocumentException {
        Document document = DocumentHelper.parseText(inputString);
        if (document == null) {
            return false;
        }
        Element root = document.getRootElement();
        if (root == null) {
            return false;
        }

        Element msgElement = root.element("MsgInfo").element("Msg");
        if (msgElement == null) {
            return false;
        }
        Iterator<Element> it = msgElement.elementIterator();
        while (it.hasNext()) {
            Element e = it.next();
            msgInfoMap.put(e.getName(), e.getText());
        }
        return true;
    }

    public static boolean parseApplyDetailMsg(String inputString, Map<String, String> msgInfoMap) throws DocumentException {
        Document document = DocumentHelper.parseText(inputString);
        if (document == null) {
            return false;
        }
        Element root = document.getRootElement();
        if (root == null) {
            return false;
        }

        Element msgElement = root.element("body").element("row");
        if (msgElement == null) {
            return false;
        }
        Iterator<Element> it = msgElement.elementIterator();
        while (it.hasNext()) {
            Element e = it.next();
            msgInfoMap.put(e.getName(), e.getText());
        }
        return true;
    }


    public static void main(String[] args) {
        String inputString = "<ESBEntry><MessageHeader><Fid>PS35012</Fid><OrderNo>PS35012S04001</OrderNo><SourceSysCode>S01</SourceSysCode><TargetSysCode>S04</TargetSysCode><ReturnFlag>0</ReturnFlag><MsgDate>2020-05-18 20:14:07</MsgDate></MessageHeader><MsgInfo><Msg id=\"1\" lastUpdate=\"2020-05-18 20:14:07\" action=\"insert\"><ELECTR_REQUISITION_NO>51537240</ELECTR_REQUISITION_NO><PAT_INDEX_NO>943356</PAT_INDEX_NO><MOTHER_INDEX_NO/><OUTHOSP_INDEX_NO>11446387</OUTHOSP_INDEX_NO><VISIT_CARD_NO/><OUTHOSP_NO>11446387</OUTHOSP_NO><INHOSP_INDEX_NO/><INHOSP_NO/><MR_NO>0</MR_NO><HC_NO/><ORDER_NO/><ORDER_GROUP_NO/><ID_NUMBER/><PAT_NAME>陈思情</PAT_NAME><PHYSI_SEX_CODE>2</PHYSI_SEX_CODE><PHYSI_SEX_NAME>女</PHYSI_SEX_NAME><DATE_BIRTH>19990518</DATE_BIRTH><AGE>21岁</AGE><HEIGHT/><WEIGHT/><NATIVE_ADDR>福建省宁化县</NATIVE_ADDR><FEE_CATEG_CODE>1</FEE_CATEG_CODE><FEE_CATEG_NAME>自费</FEE_CATEG_NAME><CONTACT_ADDR>福建省三明市宁化县新庙段</CONTACT_ADDR><CONTACT_ADDR_ZIP_CODE/><PRIORITY_FLAG>0</PRIORITY_FLAG><REQUISITION_NAME/><EXAM_PAT_RESOURCE_CODE/><EXAM_PAT_RESOURCE_NAME/><EXAM_CATEG_CODE>12</EXAM_CATEG_CODE><EXAM_CATEG_NAME>其他</EXAM_CATEG_NAME><EXAM_SUB_CATEG_CODE/><EXAM_SUB_CATEG_NAME/><EXAM_GROUP_CODE/><EXAM_GROUP_NAME/><APPLY_DEPT_CODE>91</APPLY_DEPT_CODE><APPLY_DEPT_NAME>急诊科门诊</APPLY_DEPT_NAME><APPLY_DR_CODE>742</APPLY_DR_CODE><APPLY_DR_NAME>张恩星</APPLY_DR_NAME><APPLY_WARD_CODE/><APPLY_WARD_NAME/><BED_CODE/><BED_NAME/><APPLY_DATE>20200518200031</APPLY_DATE><RESERVE_EXAM_DATE/><CLINIC_SYMPTOM/><DIAG_CODE/><DIAG_NAME/><EXAM_AIM/><NOTE/><EXECUT_DEPT_CODE>66</EXECUT_DEPT_CODE><EXECUT_DEPT_NAME>急诊科</EXECUT_DEPT_NAME><TOTAL_MONEY>27</TOTAL_MONEY><CHARGE_FLAG>1</CHARGE_FLAG><EXAM_PART_DESCR/><REQUISITION_STATUS>1</REQUISITION_STATUS><EXAM_APPLY_ITEM_CODE>62457</EXAM_APPLY_ITEM_CODE><EXAM_APPLY_ITEM_NAME>床边心电图(12导)(病区)</EXAM_APPLY_ITEM_NAME><CHARGE_DATE>20200518200033</CHARGE_DATE><HOSPITAL_DISTRICT_FLAG>1</HOSPITAL_DISTRICT_FLAG></Msg></MsgInfo></ESBEntry>";

        System.out.println("inputString = " + inputString);
        inputString = inputString.replaceAll("<Msg .+action=\"insert\">", "<Msg><![CDATA[");
        inputString = inputString.replaceAll("</Msg>", "]]></Msg>");
        System.out.println();
        System.out.println("inputString2 = " + inputString);
    }
}